<script setup>
import { ref } from 'vue'
import useUserInfoStore from '@/stores/userInfo.js'
const userInfoStore=useUserInfoStore()
const userInfo = ref({...userInfoStore.info})

//校验密码函数
const checkRePassword=(rule,value,callback)=>{
    if(value==''){
        callback(new Error('请再次输入密码'))
    }else if (value!=userInfo.value.newpassword){
        callback(new Error('两次密码不一致！'))
    }else{
        callback()
    }

}
const rules = {
    oldpassword:[
        {required:true,message:"请输入旧密码",trigger:'blur'},
        {min:5,max:16,message:"长度为5~16位非空字符",trigger:'blur'}
    ],
    newpassword:[
    {required:true,message:"请输入新密码",trigger:'blur'},
    {min:5,max:16,message:"长度为5~16位非空字符",trigger:'blur'}
    ],
    rePassword:[
    {validator:checkRePassword,trigger:'blur'}
    ],
}

// //调用后台接口，完成用户密码修改
import { userPasswordUpdateService } from '@/api/user'
import { ElMessage } from 'element-plus'
const updateUserPassword=async()=>{
    //调用接口
     let result=await userPasswordUpdateService(userInfo.value);
     ElMessage.success(result.msg? result.msg:'修改成功');
     //修改pinia中的个人信息
     userInfoStore.setInfo(userInfo.value)
}



</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码" prop="oldpassword">
                       <el-input v-model="userInfo.oldpassword" ></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpassword">
                        <el-input v-model="userInfo.newpassword"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="rePassword">
                        <el-input v-model="userInfo.rePassword"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserPassword">确认修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>